<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{for}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.extends.html" title="{extends}">
<link rel="next" href="language.function.foreach.html" title="{foreach},{foreachelse}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{for}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.extends.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.foreach.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{for}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.for"></a>{for}</h2></div></div></div>
<p>
  The <code class="varname">{for}{forelse}</code> tag is used to create simple loops. The following different formarts are supported:
 </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
    <code class="varname">{for $var=$start to $end}</code> simple loop with step size of 1.
   </p></li>
<li class="listitem"><p>
    <code class="varname">{for $var=$start to $end step $step}</code> loop with individual step size.
   </p></li>
</ul></div>
<p>
  <code class="varname">{forelse}</code> is executed when the loop is not iterated.
 </p>
<p><span class="bold"><strong>Attributes:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Attribute Name</th>
<th align="center">Shorthand</th>
<th align="center">Type</th>
<th align="center">Required</th>
<th align="center">Default</th>
<th>Description</th>
</tr></thead>
<tbody><tr>
<td align="center">max</td>
<td align="center">n/a</td>
<td align="center">integer</td>
<td align="center">No</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Limit the number of iterations</td>
</tr></tbody>
</table></div>
<p><span class="bold"><strong>Option Flags:</strong></span></p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Name</th>
<th>Description</th>
</tr></thead>
<tbody><tr>
<td align="center">nocache</td>
<td>Disables caching of the <code class="varname">{for}</code> loop</td>
</tr></tbody>
</table></div>
<div class="example">
<a name="id370619"></a><p class="title"><b>Example 7.27. A simple <code class="varname">{for}</code> loop</b></p>
<div class="example-contents">
<pre class="programlisting">

&lt;ul&gt;
{for $foo=1 to 3}
    &lt;li&gt;{$foo}&lt;/li&gt;
{/for}
&lt;/ul&gt;

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

&lt;ul&gt;
    &lt;li&gt;1&lt;/li&gt;
    &lt;li&gt;2&lt;/li&gt;
    &lt;li&gt;3&lt;/li&gt;
&lt;/ul&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id370649"></a><p class="title"><b>Example 7.28. Using the <code class="varname">max</code> attribute</b></p>
<div class="example-contents">
<pre class="programlisting">

$smarty-&gt;assign('to',10);

  </pre>
<pre class="programlisting">

&lt;ul&gt;
{for $foo=3 to $to max=3}
    &lt;li&gt;{$foo}&lt;/li&gt;
{/for}
&lt;/ul&gt;

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

&lt;ul&gt;
    &lt;li&gt;3&lt;/li&gt;
    &lt;li&gt;4&lt;/li&gt;
    &lt;li&gt;5&lt;/li&gt;
&lt;/ul&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id370686"></a><p class="title"><b>Example 7.29. Excution of <code class="varname">{forelse}</code></b></p>
<div class="example-contents">
<pre class="programlisting">

$smarty-&gt;assign('start',10);
$smarty-&gt;assign('to',5);

  </pre>
<pre class="programlisting">

&lt;ul&gt;
{for $foo=$start to $to}
    &lt;li&gt;{$foo}&lt;/li&gt;
{forelse}
  no iteration
{/for}
&lt;/ul&gt;

  </pre>
<p>
   The above example will output:
  </p>
<pre class="screen">

  no iteration

  </pre>
</div>
</div>
<br class="example-break"><p>
   See also <a class="link" href="language.function.foreach.html" title="{foreach},{foreachelse}"><code class="varname">{foreach}</code></a>,
   <a class="link" href="language.function.section.html" title="{section},{sectionelse}"><code class="varname">{section}</code></a>
   and
   <a class="link" href="language.function.while.html" title="{while}"><code class="varname">{while}</code></a>
  </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.extends.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.foreach.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{extends} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {foreach},{foreachelse}</td>
</tr>
</table>
</div>
</body>
</html>
